package com.glory.brepda.activity.mfg;

import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.brepda.R;
import com.glory.brepda.adapter.MantissaLotListAdapter;
import com.glory.brepda.adapter.SampleListAdapter;
import com.glory.brepda.base.BaseActivity;
import com.glory.brepda.base.Constant;
import com.glory.brepda.entity.LotList;
import com.glory.brepda.entity.SimpleEntity;
import com.glory.brepda.entity.response.BaseResponseBean;
import com.glory.brepda.utils.NetUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_CommonCallback;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DialogUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_LogUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_Utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.OnClick;

/**
 * FileName: FTTrackOutBiandaiSuccessActivity
 * Author: XJH
 * Date: 2020/5/22 9:53
 * Description:编带出站正常界面
 * History:
 */
public class FTTrackOutBiandaiSuccessActivity extends BaseActivity {


    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.tvLotID)
    TextView tvLotID;
    @BindView(R.id.tvEqpID)
    TextView tvEqpID;
    @BindView(R.id.tvLotNum)
    TextView tvLotNum;
    @BindView(R.id.rvReelLotList)
    RecyclerView rvReelLotList;
    @BindView(R.id.list_titleLayout)
    LinearLayout listTitleLayout;
    @BindView(R.id.rvBINList)
    RecyclerView rvBINList;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.cbTrackOut)
    CheckBox cbTrackOut;
    private String isCheck = "N";
    private SampleListAdapter mSampleListAdapter;
    private MantissaLotListAdapter mMantissaLotListAdapter;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_track_out_biandai_success_ft;
    }

    @Override
    protected void initView() {
        addActivity(this);
        Bundle bundle = getIntent().getExtras();
        tvTitle.setText("正常结束-(选择BIN)");
        tvBaseSubmit.setText("下一步");
        setMainLayout(llMain);
        tvLotID.setText(bundle.getString("LOTID"));
        tvEqpID.setText(bundle.getString("EQPID"));
        tvLotNum.setText(bundle.getString("LOTNUM"));
        initRecyclerView(rvBINList, this);
        initRecyclerView(rvReelLotList, this);
        mSampleListAdapter = new SampleListAdapter(null, true);
        mSampleListAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        mSampleListAdapter.isFirstOnly(true);
        rvBINList.setAdapter(mSampleListAdapter);
        mSampleListAdapter.setMainQTY(tvLotNum.getText().toString().equals("") ? 0 : Integer.parseInt(tvLotNum.getText().toString()));
        mMantissaLotListAdapter = new MantissaLotListAdapter(null, false);
        mMantissaLotListAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        mMantissaLotListAdapter.isFirstOnly(true);
        rvReelLotList.setAdapter(mMantissaLotListAdapter);
    }

    @Override
    protected void initData() {
        FreeUI_EntityCallBack<BaseResponseBean<SimpleEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<SimpleEntity>>
                (new TypeToken<BaseResponseBean<SimpleEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<SimpleEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    SimpleEntity entity = response.body().getResponse().getBody();
                    if (!entity.getTRACKSHOWMSG().equals("")) {
                        showConfirmDialog(entity.getTRACKSHOWMSG());
                    }
                    if (entity.getLOTBINDATALIST().size() <= 0) {
                        showToast("暂无BIN测试数据", false);
                        return;
                    } else if (entity.getMANTISSALOTBINDATALIST().size() <= 0) {
                        showToast("暂无尾数批次信息", false);
                    }

                    List<SimpleEntity.MantissaLotBinData> list = new ArrayList<>();
                    Map<String, SimpleEntity.MantissaLotBinData> map = new HashMap<>();
                    for (int i = 0; i < entity.getLOTBINDATALIST().size(); i++) {
                        if (map.containsKey(entity.getLOTBINDATALIST().get(i).getBINNAME())) {
                            SimpleEntity.MantissaLotBinData mantissaLotBinData1 = entity.getLOTBINDATALIST().get(i);
                            SimpleEntity.MantissaLotBinData mantissaLotBinData = map.get(entity.getLOTBINDATALIST().get(i).getBINNAME());
                            mantissaLotBinData.setMAINQTY(mantissaLotBinData.getMAINQTY() + mantissaLotBinData1.getMAINQTY());
                            map.put(entity.getLOTBINDATALIST().get(i).getBINNAME(), mantissaLotBinData);
                        } else {
                            map.put(entity.getLOTBINDATALIST().get(i).getBINNAME(), entity.getLOTBINDATALIST().get(i));
                        }
                    }

                    for (Map.Entry<String, SimpleEntity.MantissaLotBinData> entry : map.entrySet()) {
                        list.add(entry.getValue());
                    }
                    mSampleListAdapter.setNewData(list);
                    mMantissaLotListAdapter.setNewData(entity.getMANTISSALOTBINDATALIST());
                }


            }

            @Override
            public void onError(Response<BaseResponseBean<SimpleEntity>> response) {
                super.onError(response);
                loadError(response.getException(), "LOTFTTRACKOUT");
            }
        };

        JsonObject map = new JsonObject();
        map.addProperty("ACTIONTYPE", "SelectBinData");
        map.addProperty("EQPID", tvEqpID.getText().toString());
        List<LotList> lotList = new ArrayList<>();
        lotList.add(new LotList(tvLotID.getText().toString()));
        map.add("LOTLIST", new JsonParser().parse(new Gson().toJson(lotList)).getAsJsonArray());
        NetUtils.requestPostNet("IF.LOTFTTRACKOUT", "LOTFTTRACKOUT", FTTrackOutBiandaiSuccessActivity.this, map, callBack);
    }

    @OnClick({R.id.tvBaseSubmit, R.id.cbTrackOut})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.cbTrackOut:
                if (cbTrackOut.isChecked()){
                    isCheck = "Y";
                }else {
                    isCheck = "N";
                }
                break;
            case R.id.tvBaseSubmit:

                if (mSampleListAdapter.getSelectorData() == -1) {
                    showToast("请选择BIN信息", false);
                    return;
                }

                showSubmitDialog(new onSubmitListener() {
                    @Override
                    public void setOnSubmitListener() {
                        FreeUI_EntityCallBack<BaseResponseBean<SimpleEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<SimpleEntity>>
                                (new TypeToken<BaseResponseBean<SimpleEntity>>() {
                                }.getType(), getSupportFragmentManager(), this) {

                            @Override
                            public void onSuccess(Response<BaseResponseBean<SimpleEntity>> response) {
                                super.onSuccess(response);
                                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                                    SimpleEntity body = response.body().getResponse().getBody();
                                    if (!body.getTRACKSHOWMSG().equals("")) {
                                        FreeApi_DialogUtils.getConfirmDialog(FTTrackOutBiandaiSuccessActivity.this, "確定", body.getTRACKSHOWMSG(), new FreeUI_CommonCallback() {
                                            @Override
                                            public void onSuccess() {

                                                Bundle bundle = new Bundle();
                                                bundle.putString("LOTID", tvLotID.getText().toString());
                                                bundle.putString("EQPID", tvEqpID.getText().toString());
                                                bundle.putString("LOTNUM", tvLotNum.getText().toString());
                                                bundle.putString("isCheck", isCheck);
                                                bundle.putString("REELNUM", String.valueOf(body.getREELNUM()));
                                                bundle.putString("MANTISSAFLAG", body.getMANTISSAFLAG());
                                                bundle.putString("BINNAME", mSampleListAdapter.getData().get(mSampleListAdapter.getSelectorData()).getBINNAME());
                                                if (mMantissaLotListAdapter.getSelectorData() != -1) {
                                                    if (mMantissaLotListAdapter.getData().get(mMantissaLotListAdapter.getSelectorData()) != null) {
                                                        bundle.putSerializable("lotbindatalist", mMantissaLotListAdapter.getData().get(mMantissaLotListAdapter.getSelectorData()));
                                                    }
                                                }
                                                if (mSampleListAdapter.getSelectorData() != -1) {
                                                    if (mSampleListAdapter.getData().get(mSampleListAdapter.getSelectorData()) != null) {
                                                        bundle.putSerializable("SELECTORDATA", mSampleListAdapter.getData().get(mSampleListAdapter.getSelectorData()));
                                                    }
                                                }
                                                FreeApi_Utils.skipWithExtraIntent("", bundle, FTTrackOutBiandaiSuccessActivity.this, body.getPACKAGETYPE().equals("REEL") ?
                                                        FTTrackOutBiandaiSuccess3Activity.class : FTTrackOutBiandaiSuccess2Activity.class);
                                                return;
                                            }
                                        }).show(getSupportFragmentManager(), "MSG_DIALOG");
                                    }

                                    Bundle bundle = new Bundle();
                                    bundle.putString("LOTID", tvLotID.getText().toString());
                                    bundle.putString("EQPID", tvEqpID.getText().toString());
                                    bundle.putString("isCheck", isCheck);
                                    bundle.putString("LOTNUM", tvLotNum.getText().toString());
                                    bundle.putString("REELNUM", String.valueOf(body.getREELNUM()));
                                    bundle.putString("MANTISSAFLAG", body.getMANTISSAFLAG());
                                    bundle.putString("BINNAME", mSampleListAdapter.getData().get(mSampleListAdapter.getSelectorData()).getBINNAME());
                                    if (mMantissaLotListAdapter.getSelectorData() != -1){
                                        bundle.putSerializable("lotbindatalist", mMantissaLotListAdapter.getData().get(mMantissaLotListAdapter.getSelectorData()));
                                    }
                                    if (mSampleListAdapter.getSelectorData() != -1){
                                        bundle.putSerializable("SELECTORDATA", mSampleListAdapter.getData().get(mSampleListAdapter.getSelectorData()));
                                    }
                                     FreeApi_Utils.skipWithExtraIntent("", bundle, FTTrackOutBiandaiSuccessActivity.this, body.getPACKAGETYPE().equals("REEL") ?
                                            FTTrackOutBiandaiSuccess3Activity.class : FTTrackOutBiandaiSuccess2Activity.class);
//                            finish();
                                }
                            }

                            @Override
                            public void onError(Response<BaseResponseBean<SimpleEntity>> response) {
                                super.onError(response);
                                loadError(response.getException(), "LOTFTTRACKOUT");
                            }
                        };
                        JsonObject map = new JsonObject();
                        map.addProperty("ACTIONTYPE", "GuideTrackout");
                        map.addProperty("EQUIPMENTID", tvEqpID.getText().toString());
                        List<LotList> lotList = new ArrayList<>();
                        lotList.add(new LotList(tvLotID.getText().toString()));
                        map.add("LOTLIST", new JsonParser().parse(new Gson().toJson(lotList)).getAsJsonArray());
                        List<SimpleEntity.MantissaLotBinData> list = new ArrayList<>();
                        list.add(mSampleListAdapter.getData().get(mSampleListAdapter.getSelectorData()));
                        map.add("LOTBINDATALIST", new JsonParser().parse(new Gson().toJson(list)).getAsJsonArray());
                        NetUtils.requestPostNet("IF.LOTFTTRACKOUT", "LOTFTTRACKOUT", FTTrackOutBiandaiSuccessActivity.this, map, callBack);
                    }
                }, "是否进行下一步");
                break;
        }
    }

}
